﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForMOUpdated : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //生产订单
            UFIDA.U9.MO.MO.MO mo = key.GetEntity() as UFIDA.U9.MO.MO.MO;
            if (mo == null) return;

            #region 生产订单三个私有字段(5(备料数),6(领料申请数量),7(领料出库数量)),基于生产订单备料三个字段(备料数量,领料申请数,领料出库数)求和
            using (ISession session = Session.Open())
            {
                bool ischange = false;
                foreach (var line in mo.MOPickLists)
                {
                    if (line.OriginalData.ActualReqQty != line.ActualReqQty)
                    {
                        ischange = true;
                        break;
                    }
                }

                if (ischange)
                {
                    //备料数量
                    string sql = $@"select sum(line.ActualReqQty) acqty from MO_MO mo 
                                left join MO_MOPickList line on line.MO = mo.ID
                                where mo.DocNo = '{mo.DocNo}'
                                group by mo.DocNo ";
                    DataSet ds = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql, null, out ds);
                    if (ds.Tables[0].Rows == null || ds.Tables[0].Rows.Count <= 0)
                        mo.DescFlexField.PrivateDescSeg5 = "";
                    else
                        mo.DescFlexField.PrivateDescSeg5 = ds.Tables[0].Rows[0][0].ToString();
                }

                ischange = false;
                foreach (var line in mo.MOPickLists)
                {
                    if (line.IssueApplyQty != line.OriginalData.IssueApplyQty)
                    {
                        ischange = true;
                        break;
                    }
                }
                if (ischange)
                {
                    //领料申请数量
                    string sql1 = $@"select sum(line.IssueApplyQty) appqty from MO_MO mo 
                                left join MO_MOPickList line on line.MO = mo.ID
                                where mo.DocNo = '{mo.DocNo}'
                                group by mo.DocNo  ";
                    DataSet ds1 = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql1, null, out ds1);
                    if (ds1.Tables[0].Rows == null || ds1.Tables[0].Rows.Count <= 0)
                        mo.DescFlexField.PrivateDescSeg6 = "";
                    else
                        mo.DescFlexField.PrivateDescSeg6 = ds1.Tables[0].Rows[0][0].ToString();
                }

                ischange = false;
                foreach (var line in mo.MOPickLists)
                {
                    if (line.IssuedQty != line.OriginalData.IssuedQty)
                    {
                        ischange = true;
                        break;
                    }
                }
                if (ischange)
                {
                    //领料出库数量
                    string sql2 = $@"select sum(line.IssuedQty) issqty  from MO_MO mo 
                                left join MO_MOPickList line on line.MO = mo.ID
                                where mo.DocNo = '{mo.DocNo}'
                                group by mo.DocNo  ";
                    DataSet ds2 = null;
                    DataAccessor.RunSQL(DataAccessor.GetConn(), sql2, null, out ds2);
                    if (ds2.Tables[0].Rows == null || ds2.Tables[0].Rows.Count <= 0)
                        mo.DescFlexField.PrivateDescSeg7 = "";
                    else
                        mo.DescFlexField.PrivateDescSeg7 = ds2.Tables[0].Rows[0][0].ToString();

                }
                session.Commit();
            }
            // }



            #endregion

        }

    }
}
